package ru.nsu.fit.ws.registration;

import ru.nsu.fit.shared.data_base_manager.DatabaseManager;

import javax.jws.HandlerChain;
import javax.jws.WebService;
import java.sql.SQLException;

import static ru.nsu.fit.shared.error_codes.ErrorCodes.*;

/**
 * Created by IntelliJ IDEA.
 * User: vtimofeev
 * Date: 11/15/12
 * Time: 11:46 PM
 * To change this template use File | Settings | File Templates.
 */
@WebService(endpointInterface = "ru.nsu.fit.ws.registration.Registration")
@HandlerChain(file= "handler-chain.xml")
public class RegistrationImpl implements Registration{
    //@Override
    public RegistrationOutput registerUser(RegistrationInput registrationInput) {

        RegistrationOutput registrationOutput = new RegistrationOutput();
        String login = registrationInput.getLogin();
        String password = registrationInput.getPassword();

        try{

            boolean userAlreadyExists = DatabaseManager.isLoginExistent(login);

            if (userAlreadyExists) {
                registrationOutput.setSuccessful(false);
                registrationOutput.setErrorMessage(EXISTENT_USER.getMessage());
                return registrationOutput;
            }
            else {
                DatabaseManager.addUser(login, password);
            }
        }
        catch (SQLException s){
            s.printStackTrace();
            registrationOutput.setSuccessful(false);
            registrationOutput.setErrorMessage( DATABASE_ERROR.getMessage());
            return registrationOutput;
        }

        registrationOutput.setSuccessful(true);
        registrationOutput.setErrorMessage(NO_ERROR.getMessage());
        return registrationOutput;
    }
}
